/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vista;

import dao.UsuarioDao;
import dao.imp.UsuarioDaoImp;
import javax.swing.JOptionPane;
import modelo.dominio.Usuario;

/**
 *
 * @author Pablo
 */
public class UILogin extends javax.swing.JDialog {
                UsuarioDao ud = new UsuarioDaoImp();
            

    /**
     * Creates new form UILogin
     */
    public UILogin(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    
    }
     public UILogin(java.awt.Frame parent, boolean modal, Usuario usuario) {
        super(parent, modal);
        initComponents();
    
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtNombre = new javax.swing.JTextField();
        pswPass = new javax.swing.JPasswordField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        btnEntrar = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Login");
        setAlwaysOnTop(true);
        setBackground(java.awt.Color.black);
        setForeground(new java.awt.Color(51, 51, 255));

        txtNombre.setForeground(new java.awt.Color(51, 51, 255));

        pswPass.setForeground(new java.awt.Color(102, 102, 255));
        pswPass.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                login(evt);
            }
        });

        jLabel1.setText("Usuario:");

        jLabel2.setText("Contraseña:");

        btnEntrar.setText("Entrar");
        btnEntrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEntrarActionPerformed(evt);
            }
        });

        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel3.setBackground(new java.awt.Color(102, 102, 255));
        jLabel3.setFont(new java.awt.Font("Rod", 1, 36)); // NOI18N
        jLabel3.setText("TL51");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(15, 15, 15)
                                .addComponent(btnEntrar, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(32, 32, 32)
                                .addComponent(jButton2))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(pswPass, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)
                                    .addComponent(txtNombre)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(79, 79, 79)
                        .addComponent(jLabel3)))
                .addContainerGap(43, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(pswPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnEntrar)
                    .addComponent(jButton2))
                .addContainerGap(58, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void btnEntrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEntrarActionPerformed
        // TODO add your handling code here:
             String nom=txtNombre.getText();
             String pass=String.valueOf(pswPass.getPassword());                     
             Usuario us = new UsuarioDaoImp().getUsuarioByLogin(nom, pass);
                            
             if(us != null) {                
                 if(us.getPermisos().equals("Administrador")){    
                  UIMenu menu = new UIMenu(us.getNombre()+" "+us.getApellido());
                  
                  menu.setExtendedState(menu.MAXIMIZED_BOTH);
                  menu.setLocationRelativeTo(null);
                  menu.setVisible(true);   
                  this.dispose();         
                 }        
                    
                 else {  
                  UIMenu menu = new UIMenu(us);
                  
                  menu.setExtendedState(menu.MAXIMIZED_BOTH);
                  menu.setLocationRelativeTo(null);
                 
                  menu.setVisible(true);   
                  this.dispose(); 
                 
            } 
           }else {
                 JOptionPane.showMessageDialog(this, "Usuario o Constraseña Incorrectos");
                     txtNombre.setText(null);
                     pswPass.setText(null);   
             }
                                                            
    }//GEN-LAST:event_btnEntrarActionPerformed

    private void login(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_login
        // TODO add your handling code here:
         if (evt.getKeyCode() == evt.VK_ENTER){
              String nom=txtNombre.getText();
             String pass=String.valueOf(pswPass.getPassword());                     
             Usuario us = new UsuarioDaoImp().getUsuarioByLogin(nom, pass);
                            
             if(us != null) {                
                 if(us.getPermisos().equals("Administrador")){    
                  UIMenu menu = new UIMenu(us.getNombre()+" "+us.getApellido());
                  
                  menu.setExtendedState(menu.MAXIMIZED_BOTH);
                  menu.setLocationRelativeTo(null);
                  menu.setVisible(true);   
                  this.dispose();         
                 }        
                    
                 else {  
                  UIMenu menu = new UIMenu(us);
                  
                  menu.setExtendedState(menu.MAXIMIZED_BOTH);
                  menu.setLocationRelativeTo(null);
                 
                  menu.setVisible(true);   
                  this.dispose(); 
                 
            } 
           }else {
                 JOptionPane.showMessageDialog(this, "Usuario o Constraseña Incorrectos");
                     txtNombre.setText(null);
                     pswPass.setText(null);   
             }                     
                
         }
    }//GEN-LAST:event_login

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(UILogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(UILogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(UILogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(UILogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        /*
         * Create and display the dialog
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                UILogin dialog = new UILogin(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEntrar;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPasswordField pswPass;
    private javax.swing.JTextField txtNombre;
    // End of variables declaration//GEN-END:variables
/*
  private boolean validar (String user, String pass,ArrayList<Usuario> lista){
         boolean ok = false;
             for (Usuario usuario : lista){
                        if (usuario.getNombre().equals(this.txtNombre.getText()) &&  usuario.getPass().equals(String.valueOf(pswPass.getPassword()))){
               ok = true;
                }
             }
                    return ok;
        }
  
  private boolean validarAdmin(ArrayList<Usuario> lista){
            boolean admin=false;
            String ad="Administrador";
            for(Usuario user : lista){
                if(user.getPermisos().equals(ad)){
                    admin=true;
                   
                }
                    
                                           
            }
             return admin;
            
  }
  * 
  */
}
